import { _decorator, Component, instantiate,find, Node, Prefab, Sprite, tween, Color } from 'cc';
import { GameMgr } from '../GameMgr';
import { Random } from '../../mathAlgo/Random';
const { ccclass, property } = _decorator;

@ccclass('DropProp')
export class DropProp extends Component {


    createProp() {
        let randomProp:Prefab=Random.RandomItem( GameMgr.Inst.propGoList);
        let n =instantiate(randomProp);
        let {x,y}=this.node.getPosition();
        n.setPosition(x,y);

        find("Canvas/props").addChild(n);
    }

    protected onDestroy(): void {
       if(GameMgr.Inst.gameOverState===false){

            this.createProp();
        }
    }

    private sp:Sprite|null=null;
    start() {
        this.sp =this.getComponent(Sprite);

        //这里forever会自动释放tween吗
        tween(this.sp)
        .delay(Random.Range2(0.1,0.35))
        .to(0,{color:Color.WHITE.clone()})
        .delay(Random.Range2(0.1,0.35))
        .to(0,{color:Color.RED.clone()})
        .union()
        .repeatForever()
        .start();
    }

}


